package com.abel.quiz.common;/**
 * Created by abel.lin on 2017/9/4.
 */

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * @author abel.lin
 * @date_time 2017/9/4 11:03
 * @description
 */
public class DateUtil {
    public static final String FORMAT_YMD = "yyyy-MM-dd";
    public static final String FORMAT_YMDHM = "yyyy-MM-dd HH:mm";
    public static final String FORMAT_YMDHMS = "yyyy-MM-dd HH:mm:ss";

    /**
     * @param
     * @description 时间转字符，格式：yyyy-MM-dd HH:mm:ss
     * @author abel.lin
     * @date_time 2017/9/4 11:23
     */
    public static String dateToStr(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat(FORMAT_YMDHMS);
        String dateString = formatter.format(date);
        return dateString;
    }
    /**
     * @param
     * @description 时间转字符，格式：yyyy-MM-dd HH:mm
     * @author abel.lin
     * @date_time 2017/9/4 11:23
     */
    public static String dateToStrYMDHM(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat(FORMAT_YMDHM);
        String dateString = formatter.format(date);
        return dateString;
    }
    /**
     * @param
     * @description 时间转字符，格式：yyyy-MM-dd
     * @author abel.lin
     * @date_time 2017/9/4 11:23
     */
    public static String dateToStrYMD(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat(FORMAT_YMD);
        String dateString = formatter.format(date);
        return dateString;
    }

    /**
     * @param
     * @description 字符转时间，格式：yyyy-MM-dd HH:mm:ss
     * @author abel.lin
     * @date_time 2017/9/4 11:24
     */
    public static Date strToDate(String strDate) {
        SimpleDateFormat formatter = new SimpleDateFormat(FORMAT_YMDHMS);
        ParsePosition pos = new ParsePosition(0);
        Date date = formatter.parse(strDate, pos);
        return date;
    }
    /**
     * @param
     * @description 字符转时间，格式：yyyy-MM-dd HH:mm
     * @author abel.lin
     * @date_time 2017/9/4 11:24
     */
    public static Date strToDateYMDHM(String strDate) {
        SimpleDateFormat formatter = new SimpleDateFormat(FORMAT_YMDHM);
        ParsePosition pos = new ParsePosition(0);
        Date date = formatter.parse(strDate, pos);
        return date;
    }
    /**
     * @param
     * @description 字符转时间，格式：yyyy-MM-dd
     * @author abel.lin
     * @date_time 2017/9/4 11:24
     */
    public static Date strToDateYMD(String strDate) {
        SimpleDateFormat formatter = new SimpleDateFormat(FORMAT_YMD);
        ParsePosition pos = new ParsePosition(0);
        Date date = formatter.parse(strDate, pos);
        return date;
    }

    /**
     * @param
     * @description 一天中最小时间
     * @author abel.lin
     * @date_time 2017/9/4 11:27
     */
    public static Date dayBegin(String dateStr){
        Date date = null;
        try{
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            date = sdf.parse(dateStr);
        }catch(Exception e){
            date = new Date();
        }

        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        return c.getTime();
    }
    /**
     * @param
     * @description 一天中最大时间
     * @author abel.lin
     * @date_time 2017/9/4 11:28
     */
    public static Date dayEnd(String dateStr){
        Date date = null;
        try{
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            date = sdf.parse(dateStr);
        }catch(Exception e){
            date = new Date();
        }

        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.HOUR_OF_DAY, 23);
        c.set(Calendar.MINUTE, 59);
        c.set(Calendar.SECOND, 59);
        c.set(Calendar.MILLISECOND, 999);
        return c.getTime();
    }

    /**
     * @param
     * @description 本周第一天，周一
     * @author abel.lin
     * @date_time 2017/9/4 11:28
     */
    public static Date getWeekFirstDay(Date date){
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int dayOfWeek = c.get(Calendar.DAY_OF_WEEK) - 2;
        c.add(Calendar.DATE, -dayOfWeek); // 得到本周的第一天

        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        return c.getTime();
    }
    /**
     * @param
     * @description 本周最后一天，周日
     * @author abel.lin
     * @date_time 2017/9/4 11:29
     */
    public static Date getWeekLastDay(Date date){
        Calendar c = Calendar.getInstance();
        c.setTime(getWeekFirstDay(date));
        c.add(Calendar.DATE, 6); // 得到本周的最后一天
        c.set(Calendar.HOUR_OF_DAY, 23);
        c.set(Calendar.MINUTE, 59);
        c.set(Calendar.SECOND, 59);
        c.set(Calendar.MILLISECOND, 999);
        return c.getTime();
    }

    /**
     * @param
     * @description 本月第一天
     * @author abel.lin
     * @date_time 2017/9/4 11:29
     */
    public static Date getMonthFirstDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
        return calendar.getTime();
    }
    /**
     * @param
     * @description 本月最后一天
     * @author abel.lin
     * @date_time 2017/9/4 11:30
     */
    public static Date getMonthLastDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        return calendar.getTime();
    }

    /**
     * @param
     * @description 是否闰年;
     * 1.被400整除是闰年，否则不是
     * 2.不能被4整除则不是闰年
     * 3.能被4整除同时不能被100整除则是闰年
     * 4.能被4整除同时能被100整除则不是闰年
     * @author abel.lin
     * @date_time 2017/9/4 11:32
     */
    public static boolean isLeapYear(Date date) {
        GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
        gc.setTime(date);
        int year = gc.get(Calendar.YEAR);
        if ((year % 400) == 0)
            return true;
        else if ((year % 4) == 0) {
            if ((year % 100) == 0)
                return false;
            else
                return true;
        } else
            return false;
    }

    /**
     * @param
     * @description 计算日期的前后几天，n为负数时表示前n天，正数表示后n天
     * @author abel.lin
     * @date_time 2017/9/4 14:12
     */
    public static Date calculateWithDays(Date date, int n){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DAY_OF_MONTH, n);
        return cal.getTime();
    }

    /**
     * @param
     * @description 比较两个时间大小
     * @return [-1：date1小于date2，0：相等，1：date1大于date2]
     * @author abel.lin
     * @date_time 2017/9/4 14:21
     */
    public static int compare(Date date1, Date date2){
        if(date1 == null || date2 == null){
            return 0;
        }
        if(date1.getTime() - date2.getTime() > 0){
            return 1;
        }else if(date1.getTime() - date2.getTime() == 0){
            return 0;
        }else {
            return -1;
        }
    }

}
